package com.kdtech.analyse.common;

import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;

public class NewsAnalyseTool implements AnalyseNews {
	
	private final NewsAnalyseConfig config;
	

	public NewsAnalyseTool(NewsAnalyseConfig config) {
		this.config = config;
	}

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, config.getUrlRegexs());
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news = new NewsMeta();
		if (urlMeta.getHtml() == null) {
			return null;
		}		
		String url = urlMeta.getUrl();		
		String htmltxt = urlMeta.getHtml();
		news.setUrl(url);		
		Document doc = Jsoup.parse(htmltxt);
		String title = getText(url,doc,config.getTitleRegexs());
		String content = getText(url,doc,config.getContentRegexs());
		Long date = getDate(url,doc,config.getDateRegexs());
		if (date==null){
			date = DateUtils.matchDate(url);
		}
		Integer clickNum = getNumber(url,doc,config.getClickNumRegexs());
		Integer commentNum = getNumber(url,doc,config.getCommentNumRegexs());

		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);	
		news.setClickNum(clickNum);
		news.setCommentNum(commentNum);
		
		return news;
	}
	
	private String getText(String url,Document doc,List<AnalyseSelector> AnalyseSelectors)
	{
		String text = null;
		for (AnalyseSelector AnalyseSelector : AnalyseSelectors) {
			if (StringUtils.isNullOrEmpty(text)){
				text = AnalyseSelector.getAnalyseString(doc,url);
			}else{
				break;
			}
		}
		return text;
	}
	
	private Long getDate(String url,Document doc,List<AnalyseSelector> AnalyseSelectors)
	{
		Long date = null;
		for (AnalyseSelector AnalyseSelector : AnalyseSelectors) {
			date = DateUtils.matchDate(AnalyseSelector.getAnalyseString(doc,url));
			if (date!=null){
				return date;
			}
		}
		return date;
	}
	private Integer getNumber(String url,Document doc,List<AnalyseSelector> AnalyseSelectors)
	{
		Integer date = null;
		for (AnalyseSelector AnalyseSelector : AnalyseSelectors) {
			date = NumberUtils.matchNumber(AnalyseSelector.getAnalyseString(doc,url));
			if (date!=null){
				return date;
			}
		}
		return date;
	}


	

	

	
	
	public static void main(String[] args) {
		NewsAnalyseConfig config = new NewsAnalyseConfig("测试站点");
		config.setNeedUpdate(false);
		
		//URL正则
		config.getUrlRegexs().add("http://www.metrohk.com.hk/\\?cmd=detail&id=[0-9]*");
		
		//标题解析规则
		config.getTitleRegexs().add(AnalyseSelector.init().select("title").substringBefore("_"));
		
		//正文解析规则
		config.getContentRegexs().add(AnalyseSelector.init().select("div.bd"));
		
		//日期解析规则
		config.getDateRegexs().add(AnalyseSelector.init().select("span.article-time"));
		
		UrlHandler urlHandler = new UrlHandler(){
			public String getUrl(Document doc,String url){
				String cmt_id = StringUtils.substringBetween(doc.html(), "cmt_id = ",";");
				String updateUrl = "http://coral.qq.com/article/" + cmt_id + "/commentnum?callback=_cbSum&source=1&t=" + Math.random();
				return updateUrl;
			}
		};
		
		
		//浏览数解析规则
		config.getCommentNumRegexs().add(AnalyseSelector.withUrlHandler(urlHandler).substringBetween("commentnum", "}"));
		
		//评论数解析规则
//		config.getCommentNumRegexs().add(new AnalyseSelector("div.blog_bottom a:containsOwn(评论)"));
		String url = "http://news.qq.com/a/20130918/001277.htm";
		NewsAnalyseTool analyse = new NewsAnalyseTool(config);
		UrlMeta meta = CrawlHTML.responseToURL(url);
		System.out.println(analyse.isDetailPage(url));
		System.out.println(analyse.parserHtml(meta));
	}
	
	

}
